<?php
/**
 * 今日日签
 * BY：云猫
 * blog：lwcat.cn
 * QQ：3522934828
 * 项目地址https://gitee.com/ximami/daily/
 */
function resizeImage($imagePath, $newWidth, $newHeight) {
    $extension = strtolower(pathinfo($imagePath, PATHINFO_EXTENSION));
    switch ($extension) {
        case 'jpg':
        case 'jpeg':
            $image = imagecreatefromjpeg($imagePath);
            break;
        case 'png':
            $image = imagecreatefrompng($imagePath);
            break;
        case 'gif':
            $image = imagecreatefromgif($imagePath);
            break;
        default:
            return false;
    }

    if (!$image) return false;

    $width = imagesx($image);
    $height = imagesy($image);

    $newImage = imagecreatetruecolor($newWidth, $newHeight);
    imagecopyresampled($newImage, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);

    switch ($extension) {
        case 'jpg':
        case 'jpeg':
            imagejpeg($newImage, $imagePath);
            break;
        case 'png':
            imagepng($newImage, $imagePath);
            break;
        case 'gif':
            imagegif($newImage, $imagePath);
            break;
    }

    imagedestroy($image);
    imagedestroy($newImage);
    return $imagePath;
}

function addTextToImage($imagePath, $textParams) {
    header('Content-Type: image/jpeg');
    $image = imagecreatefromjpeg($imagePath);
    $width = imagesx($image);
    $height = imagesy($image);
    $fontPath = 'font.ttf';

    foreach ($textParams as $textParam) {
        $text = $textParam['text'];
        $fontSize = $textParam['size'];
        $position = $textParam['position'];
        $color = $textParam['color'];
        $positionsite = $textParam['positionsite'];
        $xOffset = $textParam['x_offset'] ?? 0;
        $textColor = imagecolorallocate($image, $color[0], $color[1], $color[2]);

        $lines = [];
        $words = explode(' ', $text);
        $line = '';

        foreach ($words as $word) {
            $testLine = $line . $word . ' ';
            $bbox = imagettfbbox($fontSize, 0, $fontPath, $testLine);
            $testWidth = $bbox[2] - $bbox[0];
            if ($testWidth > $width) {
                $lines[] = trim($line);
                $line = $word . ' ';
            } else {
                $line = $testLine;
            }
        }
        $lines[] = trim($line);

        $textHeight = ($bbox[1] - $bbox[7]) * count($lines);
        switch ($position) {
            case 'top':
                $y = $positionsite;
                break;
            case 'middle':
                $y = ($height / 2) - ($textHeight / 2) + ($bbox[1] - $bbox[7]);
                break;
            case 'bottom':
            default:
                $y = $height - $textHeight - $positionsite;
                break;
        }

        foreach ($lines as $line) {
            $bbox = imagettfbbox($fontSize, 0, $fontPath, $line);
            $textWidth = $bbox[2] - $bbox[0];
            if ($xOffset < 0) {
                $x = -$xOffset;
            } elseif ($xOffset > 0) {
                $x = $width - $textWidth - $xOffset;
            } else {
                $x = ($width - $textWidth) / 2;
            }
            imagettftext($image, $fontSize, 0, $x, $y, $textColor, $fontPath, $line);
            $y += $bbox[1] - $bbox[7];
        }
    }

    imagejpeg($image);
    imagedestroy($image);
}

function curlGetRequest($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    $response = curl_exec($ch);
    if (curl_errno($ch)) {
        return 'cURL error: ' . curl_error($ch);
    }
    curl_close($ch);
    return $response;
}

$day = json_decode(curlGetRequest("https://api.lwcat.cn/api/day/"), true);
$textParams = [
    [
        'text' => $day['dateD'],
        'size' => 180,
        'position' => 'bottom',
        'positionsite' => 600,
        'x_offset' => -80,
        'color' => [255, 255, 255]
    ],
    [
        'text' => $day['dateMC'].'月  '.$day['dateY'],
        'size' => 50,
        'position' => 'bottom',
        'positionsite' => 590,
        'x_offset' => -80,
        'color' => [255, 255, 255]
    ],
    [
        'text' => $day['hseb'],
        'size' => 50,
        'position' => 'bottom',
        'positionsite' => 510,
        'x_offset' => -80,
        'color' => [255, 255, 255]
    ]
];

$text = mb_strlen($day['text'], 'UTF-8') > 21 ? [
    ['text' => mb_substr($day['text'], 0, 21, 'UTF-8'), 'size' => 32, 'position' => 'bottom', 'positionsite' => 300, 'x_offset' => -150, 'color' => [255, 255, 255]],
    ['text' => mb_substr($day['text'], 21, null, 'UTF-8'), 'size' => 32, 'position' => 'bottom', 'positionsite' => 250, 'x_offset' => -70, 'color' => [255, 255, 255]],
    ['text' => '—' . $day['from_who'], 'size' => 32, 'position' => 'bottom', 'positionsite' => 200, 'x_offset' => 150, 'color' => [255, 255, 255]],
] : [
    ['text' => $day['text'], 'size' => 32, 'position' => 'bottom', 'positionsite' => 300, 'x_offset' => -150, 'color' => [255, 255, 255]],
    ['text' => '——' . $day['from_who'], 'size' => 32, 'position' => 'bottom', 'positionsite' => 200, 'x_offset' => 150, 'color' => [255, 255, 255]],
];

$textParams = array_merge($textParams, $text);

$imageDir = 'images';
$images = glob($imageDir . '/*.{jpg,JPG,jpeg,png,gif}', GLOB_BRACE);
$randomImage = $images[array_rand($images)];
$randomImage = resizeImage($randomImage, 1080, 2340); // Resize the image
addTextToImage($randomImage, $textParams);
?>
